package cn.tedu.sp02.controller;

import cn.tedu.pojo.Item;
import cn.tedu.service.ItemService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import java.util.List;
import java.util.Random;
@Slf4j
@RestController
public class ItemController {
    @Autowired
    private ItemService itemService;
    //获取订单商品列表
    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException {
        List<Item> items=itemService.getItems(orderId);
        //随机延迟，百分之九十概率执行延迟
        if(Math.random()<0.9){
            //随即延迟时常0-5秒
            long i = new Random().nextInt(5000);
            log.info("延迟时常"+i);
            Thread.sleep(i);
        }
        return JsonResult.build().code(200).data(items);
    }
    /**
     * @PathVariable接受路径参数
     * 《post，put》@RequestBody 完整接受协议体*/
    //减少商品库存
    @PostMapping("/decreaseNumber")
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> items){
        itemService.decreaseNumber(items);
        return JsonResult.build().code(200).msg("减少库存成功");
    }
    @GetMapping("/favicon.ico")
    public  void  ico(){}
}
